<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    
    
    public function index(){
        
        $newestgroup= M('groups');
        $new_group=$newestgroup->order('gid desc')->limit(0,1)->find();
        $this->newgroups =$new_group;
        
        $newestpractice= M('practices');
        $new_practice=$newestpractice->order('pid desc')->join('groups ON groups.groupname=practices.groupname')->limit(0,1)->find();
        $this->newpractices =$new_practice;
        
        $groupsmodel = M('groups');
        $local_groups = $groupsmodel->order('gid')->limit(0,3)->select();
        $this->groups = $local_groups;
        
        $practicemodel = M('practices');
        $local_practice = $practicemodel->join('groups ON practices.groupname = groups.groupname')->limit(0,3)->select();
        $this->practices = $local_practice;
        
        
        $this->display();
    }
    
    public function logout(){
    
        session('users',null);
        session('admins',null);
        session('leaders',null);
    
        if(session('users')==null)
        {
            $this->success('注销成功！',U('Index/index'));
        }else  if(session('admins')==null)
        {
            $this->success('注销成功！',U('Index/index'));
        }else  if(session('leaders')==null)
        {
            $this->success('注销成功！',U('Index/index'));
        }
        else
        {
            $this->error('注销失败！');
        }
    }
    

    public function login()
    {
        $condition = array(
            'username' => $_POST['username'],
            'password'=>$_POST['password'],
            'isadmin'=>0
            //   'password' => md5(I('post.password'))
        );
        // var_dump($condition);
        $usersModel = M('Users');
        $users = $usersModel->where($condition)->find();
    
        if ($users) {
            // 是合法用户，设置session
            session('users', $users);
    
            $this->success('登录成功！',U('Home/User/index'));
    
            //$this->redirect('Index/index');
        } else {
            $this->error('用户名或密码错误');
        }
    }
    
    
    public function leaderlogin()
    {
        $condition = array(
            'username' => $_POST['username'],
            'password'=>$_POST['password'],
            'isadmin'=>0,'ismanage'=>1
            //   'password' => md5(I('post.password'))
        );
        // var_dump($condition);
        $leadersModel = M('Users');
        $leaders =  $leadersModel->where($condition)->find();
    
        if ($leaders) {
            // 是合法用户，设置session
            session('leaders', $leaders);
            $_SESSION['users']=$_SESSION['leaders'];
            $this->success('登录成功！',U('Home/Leader/index'));
    
            //$this->redirect('Index/index');
        } else {
            $this->error('用户名或密码错误');
        }
    }
    
    public function adminlogin()
    {
        $condition = array(
            'username' => $_POST['username'],
            'password'=>$_POST['password'],
            'isadmin'=>1,'ismanage'=>0
            //   'password' => md5(I('post.password'))
        );
        // var_dump($condition);
        $adminsModel = M('Users');
        $admins =  $adminsModel->where($condition)->find();
    
        if ($admins) {
            // 是合法用户，设置session
            session('admins', $admins);
            $_SESSION['users']=$_SESSION['admins'];
            $this->success('登录成功！',U('Admin/Index/users'));
    
            //$this->redirect('Index/index');
        } else {
            $this->error('用户名或密码错误');
        }
    }
    
    public function groupslist(){
        
        $groupsmodel = M('groups');
        $local_groups = $groupsmodel->order('gid')->select();
        $this->groups = $local_groups;
        $this->display();

    }
    

    public function groupInfo($gid)
    {
        $groupModel = m('groups');
        $this->groups = $groupModel->find($gid);
        $SearchGid['gid'] = $gid;
        
        $practicesmodel = m('practices');
        $local_practices = $practicesmodel->join('groups ON practices.groupname = groups.groupname')->where($SearchGid)->select();
        $this->practices = $local_practices;
       
   
        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }
    
    public function joingroup(){
        
        $memberModel=m('Members');
        $memberModel->create();
        
        
        if ($memberModel->add()) {
            $this->success('已经成功申请，请等待审批！','userInfo');
        } else {
            $this->error('修改失败');
        }
    
        
    }
    
    public function practiceslist(){
    
        $practicemodel = M('practices');
       $local_practice = $practicemodel->join('groups ON practices.groupname = groups.groupname')->select();
        $this->practices = $local_practice;
        $this->display();
    
    }
    
    public function practiceinfo($pid){
        {
            $practiceModel = m('practices');
            $this->practices = $practiceModel->find($pid);
        
            $stupracModel = m('stuprac');
            
            $SearchGid['state'] =1;
            $SearchGid['stuprac.pid'] =$pid;       
             $local_stuprac = $stupracModel->join('practices ON stuprac.pid = practices.pid ')->where($SearchGid)->select();
            
            $this->stuprac = $local_stuprac;
            $this->display();
            // 将查询到的用户信息user传递给模版updateuserinfo。html
        }
        
        
    }
     
    
    public function joinpractice(){
    
        $memberModel=m('stuprac');
        $memberModel->create();
        $memeberModel->atime = date("Y-m-d H:i:s", time());
    
        if ($memberModel->add()) {
            $this->success('已经成功申请，请等待审批！','practiceslist');
        } else {
            $this->error('修改失败');
        }
    
    
    }
    
  
 
    
 public function userInfo()
    {
        $uid=$_SESSION['users']['uid'];
        $userModel = m('Users');
        $this->user = $userModel->find($uid);
        $SearchUid['uid'] = $uid;
        
       /* $SearchUid['gid'] = $gid; where($SearchGid)->*/
        $groupModel = m('groups');
        $stunum['users.stunum']=$_SESSION['users']['stunum'];
        
        $Local_Groups = $groupModel->join('members ON groups.gid = members.gid')->join('users ON users.stunum = members.stunum')
        ->where($stunum)->select();
        $this->groups = $Local_Groups;

        
        $practicesmodel = m('practices');
        $local_practices = $practicesmodel->join('groups ON practices.groupname = groups.groupname')->join('stuprac ON stuprac.pid = practices.pid')->
        join('users ON users.stunum = stuprac.stunum')->where($stunum['users.stunum'])->select();
        
        $this->practices = $local_practices;

        $this->display();
        // 将查询到的用户信息user传递给模版updateuserinfo。html
    }
    
}